//
//  JMBasicTableViewController.m
//  仿生物无忧
//
//  Created by JimmyPeng on 15/12/9.
//  Copyright © 2015年 彭继宗. All rights reserved.
//

#import "JMBasicTableViewController.h"

#import "JMListNews.h"

#import "JMNewsCell.h"

@interface JMBasicTableViewController ()

@property (nonatomic, strong) NSArray *newsArr;

@end

@implementation JMBasicTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self prepareUI];
    //发送网络请求
    [self sendNetwork];
    
}

/**
 *  准备UI
 */
- (void)prepareUI
{
    [self.tableView registerNib:[UINib nibWithNibName:@"JMNewsCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"JMNewsCell"];
    
    self.tableView.rowHeight = 70;
    
}


#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.newsArr.count == 0)
    {
        return 10;
    }
    
    return self.newsArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JMNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JMNewsCell" forIndexPath:indexPath];
    
    if (self.newsArr.count != 0)
    {
        
        JMListNews *list = self.newsArr[indexPath.row];
        
        cell.list = list;
    }
    
    
    return cell;
}

#pragma mark- 发送网络请求
//发送网络请求
- (void)sendNetwork
{
    //发送get请求
    [[JMNetworkTools shareNetworkTools] GET:@"http://www.swwy.com/app/android/newsApi.php?authKey=a9d487eeea75356593adad64d9c55fb0&cid=46&device=4&page=1&pageSize=10" parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject){
        //下载放到自己数组中
        self.newsArr = [JMListNews listVideos:responseObject[@"data"][@"list"] ];
        //刷新tableView
        [self.tableView reloadData];
        
    } failure:nil];
}

#pragma mark- 懒加载

- (NSArray *)newsArr
{
    if (_newsArr == nil)
    {
        _newsArr = [NSArray array];
    }
    return _newsArr;
}

@end
